extends CanvasModulate


@onready var day_label: Label = %DayLabel
@onready var time_label: Label = %TimeLabel

const MINUTES_PER_DAY : int = 24 * 60
const MINUTES_PER_HOUR: int = 60
const GAME_MINUTES_DIRATION: float = TAU / MINUTES_PER_DAY

var game_speed: float = 50.0

@export var intital_day: int = 0
@export var inintal_hour: int = 12
@export var inintal_minute: int = 3

@export var days_night_gradient_texture: GradientTexture1D


var time:float = 0.0
var current_minute:int = -1
var current_day:int = 0

var days_passed = 0


func _ready() -> void:
	intital_day += days_passed
	set_intial_time()
	
func _process(delta: float) -> void:
	time += delta*game_speed*GAME_MINUTES_DIRATION
	realulate_time()
	
	
func set_intial_time():
	var intital_total_minutes = intital_day * MINUTES_PER_DAY + (inintal_hour * MINUTES_PER_HOUR) + inintal_minute
	time = intital_total_minutes*GAME_MINUTES_DIRATION
	
	
func realulate_time():
	var total_minutes:int = int(time/GAME_MINUTES_DIRATION)
	var day:int = int(total_minutes/MINUTES_PER_DAY)
	var current_day_minutes:int = total_minutes%MINUTES_PER_DAY
	var hour:int = int(current_day_minutes/MINUTES_PER_HOUR)
	var minute:int = int(current_day_minutes%MINUTES_PER_HOUR)

	
	var time_change:bool=false
	
	if current_day!=day:
		current_day = day
		time_change = true
	if current_minute!=minute:
		current_minute = minute
		time_change = true
		
		
	if time_change:
		day_label.text = "DAY:%02d"%[day]
		time_label.text = "%02d:%02d"%[hour,minute]
		
		var sample_value = hour/24.0 
		color = days_night_gradient_texture.gradient.sample(sample_value)
